package org.gg.boom.modules.schedule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.gg.boom.common.controller.BaseController;
import org.gg.boom.common.model.Response;
import org.gg.boom.modules.schedule.model.dto.ScheduleJobDTO;
import org.gg.boom.modules.schedule.service.ScheduleJobService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 */
@RestController
    @RequestMapping("/schedule")
@RequiredArgsConstructor
public class ScheduleJobController extends BaseController {

    private final ScheduleJobService scheduleJobService;

    @GetMapping("/page")
    public Response page(@RequestParam Map<String, Object> params) {
        Page<ScheduleJobDTO> page = scheduleJobService.page(params);
        return getDataTable(page);
    }

    @GetMapping("/{id}")
    public Response info(@PathVariable("id") Long id) {
        ScheduleJobDTO schedule = scheduleJobService.get(id);
        return Response.success(schedule);
    }

    @PostMapping("/add")
    public Response save(@RequestBody ScheduleJobDTO dto) {
        scheduleJobService.save(dto);
        return Response.success();
    }

    @PutMapping("/update")
    public Response update(@RequestBody ScheduleJobDTO dto) {
        scheduleJobService.update(dto);
        return Response.success();
    }

    @DeleteMapping("delete")
    public Response delete(@RequestBody Long[] ids) {
        scheduleJobService.deleteBatch(ids);
        return Response.success();
    }

    /**
     * 立即执行
     *
     * @param ids
     * @return
     */
    @PutMapping("/run")
    public Response run(@RequestBody Long[] ids) {
        scheduleJobService.run(ids);
        return Response.success();
    }

    /**
     * 暂停
     *
     * @param ids
     * @return
     */
    @PutMapping("/pause")
    public Response pause(@RequestBody Long[] ids) {
        scheduleJobService.pause(ids);
        return Response.success();
    }

    /**
     * 恢复
     *
     * @param ids
     * @return
     */
    @PutMapping("/resume")
    public Response resume(@RequestBody Long[] ids) {
        scheduleJobService.resume(ids);
        return Response.success();
    }

}
